package com.lc.headline.controller;

import com.lc.headline.common.Result;
import com.lc.headline.pojo.NewsType;
import com.lc.headline.pojo.vo.HeadlineDetailVo;
import com.lc.headline.pojo.vo.HeadlineQueryVo;
import com.lc.headline.service.NewsHeadlineService;
import com.lc.headline.service.NewsTypeService;
import com.lc.headline.service.impl.NewsHeadlineServiceImpl;
import com.lc.headline.service.impl.NewsTypeServiceImpl;
import com.lc.headline.util.WebUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;


import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liuchun
 */
@WebServlet("/portal/*")

public class PortalController extends BaseController{

    private NewsTypeService newsTypeService = new NewsTypeServiceImpl();
    private NewsHeadlineService newsHeadlineService = new NewsHeadlineServiceImpl();



    //查看头条详情
    protected void showHeadlineDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       int hid = Integer.parseInt(request.getParameter("hid"));
       Map data = new HashMap();
       HeadlineDetailVo headlineDetailVo = newsHeadlineService.showHeadline(hid);
       data.put("headline",headlineDetailVo);
       WebUtil.writeJson(response,Result.ok(data));
    }

    //分页带条件查询所有头条
    protected void findNewsPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HeadlineQueryVo headLineQueryVo = WebUtil.readJson(req, HeadlineQueryVo.class);
        // 查询分页五项数据
        Map<String,Object> pageInfo =newsHeadlineService.findPage(headLineQueryVo);
        // 将分页五项数据放入PageInfoMap
        Map<String,Object> pageInfoMap=new HashMap<>();
        pageInfoMap.put("pageInfo",pageInfo);
        // 响应JSON
        WebUtil.writeJson(resp, Result.ok(pageInfoMap));
    }



    //查询微头条所有的类型
    protected void findAllTypes(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<NewsType> newsTypeList  = newsTypeService.findAll();
        Result result = Result.ok(newsTypeList);
        WebUtil.writeJson(response,result);
    }
}
